package com.bringway.p2p.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.bringway.global.dao.P2PJdbcDao;
import com.bringway.global.dao.ZhfwJdbcDao;
import com.bringway.p2p.dao.AppRootDao;

/**
 * 
 * @author qjy
 * @version 创建时间：2016年06月14日 上午13:25:42
 * @功能：查询异常信息
 */

@Repository
public class AppRootDaoImpl  extends ZhfwJdbcDao implements AppRootDao{
	Logger logger = Logger.getLogger(AppRootDaoImpl.class);

	public List<Map<String, Object>> queryapproot(JSONObject parms) {
		int start = parms.optInt("start");
		int limit = parms.optInt("limit");
		String yhm=parms.optString("yhm");
		String xm=parms.optString("xm");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("GYH", yhm);
		paramMap.put("XM", xm);
		sql.append(" select GYH,XM,GWMC,NAME from app_root_user ");
		sql.append(" where 1=1  ");
	
		
		if(StringUtils.isNotEmpty(yhm)){
			sql.append(" AND GYH like "+ "'%"+yhm+"%'");
		}
		if(StringUtils.isNotEmpty(xm)){
			sql.append(" AND XM like "+ "'%"+xm+"%'");
		}
		
		
		logger.info("queryapproot sql is " + sql.toString());
		logger.info("queryapproot paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForList(
				sql.toString(), paramMap);

		
	}

	
public int queryGyh(JSONObject parms){
	String  yhm= parms.optString("username");
	Map<String, Object> paramMap = new HashMap<String, Object>();
	String sql=" select count(*) from  app_root_user where GYH = :yhm ";
	paramMap.put("yhm", yhm);
	logger.info("updateAppUser sql is "+sql);
	logger.info("updateAppUser paramsMap is "+paramMap);
	return this.getNamedParameterJdbcTemplate().queryForInt(sql, paramMap);
}
	
	@Override
	public int getAppRootCount(JSONObject parms) {
		int start = parms.optInt("start");
		int limit = parms.optInt("limit");
		
		String yhm = parms.optString("yhm");
		String xm=parms.optString("xm");
		
		
		StringBuffer sql = new StringBuffer();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("START", start);
		paramMap.put("LIMIT", limit);
		paramMap.put("yhm", yhm);
		
		sql.append(" SELECT count(*)  ");
		sql.append(" FROM app_root_user ");
	
		sql.append("   where 1=1 ");
	
/*		if(StringUtils.isNotEmpty(yhm)){
			sql.append(" AND GYH like "+ "'%"+yhm+"%'");
		}*/
		
		
		if (StringUtils.isNotEmpty(yhm)) {
			sql.append(" and GYH = :yhm ");
			paramMap.put("GYH", yhm);
		}
		if(StringUtils.isNotEmpty(xm)){
			sql.append(" AND XM like "+ "'%"+xm+"%'");
		}
		logger.info("getAppRootCount sql is " + sql.toString());
		logger.info("getAppRootCount paramMap is " + paramMap.toString());
		return this.getNamedParameterJdbcTemplate().queryForInt(sql.toString(), paramMap);
	}



}
